package basic;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import flexibility.userData;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import flexibility.getError;

//import com.mysql.jdbc.PreparedStatement;
import com.sun.rowset.CachedRowSetImpl;

public class executeQuery {

	private getError errorHandler = new getError();
	public CachedRowSetImpl readFromDataBase(Connection con, String query) {
		Statement stmt;
		CachedRowSetImpl crs = null;
		try {
			stmt = con.createStatement();
			crs = new CachedRowSetImpl();
			ResultSet rs = stmt.executeQuery(query);
			crs.populate(rs);//this line transfers the results of the query, which are currently stored in rs object
			rs.close();// to the crs object. we need that because you can not transfer rs object through sockets,
		} catch (SQLException e) {// but can transfer crs object through the socket
			errorHandler.printSQLException(e);
		}
		
		return crs;
	}

	public Boolean writeToDataBase(Connection con, String query) {
		Statement stmt;
		Boolean rs = null;
		//getError errorToMsg = new getError();
		//String errorCode = null;
		try {
			stmt = con.createStatement();
			 rs = stmt.execute(query);
			return rs;
		} catch (SQLException e) {
		//	errorCode = errorToMsg.error(e.getErrorCode());
			//errorCode = errorCode + "(error " + e.getErrorCode() + ")";
			return rs;
		}

	}
	
	// 'uploadFileToDataBase' function uploads the selected file to the DataBase
	public Boolean uploadFileToDataBase(Connection con, String query,userData msg) {
		
		int res=0;
		
		String fileName=(String) msg.getData(2); //get file name
		int fileSize=(Integer) msg.getData(3);  //get the size of the file to upload
		byte[] file_byte_array = new byte[fileSize];
		file_byte_array=(byte[]) msg.getData(4); //insert into byte array the data of the file
		Integer userID=(Integer) msg.getData(5); //get userID
		
		
		//Prepare and execute query of uploading file to DB 
		PreparedStatement psmnt=null;
		try {
			psmnt = con.prepareStatement(query);
			psmnt.setString(1, fileName);
			psmnt.setInt(2, fileSize);
			psmnt.setBytes(3,file_byte_array);
			psmnt.setInt(4, userID);
			if(msg.getLength()==7){//insert fileId in case the query is to update the existed file
				psmnt.setInt(5, (Integer)msg.getData(6)); 
			}
			System.out.println(fileName);
			res=psmnt.executeUpdate();
			psmnt.close();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		if (res==1)
			return true;
		else
			return false;
	}
	
	

}
